package test.core.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.nustaq.serialization.FSTConfiguration;
import org.objenesis.strategy.StdInstantiatorStrategy;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import hyl.core.conf.MyConfig;
import hyl.core.io.MyFile;
/**
 * 批量序列化反序列化工具类
 * 
 * @author 阿友 3798955@qq.com
 *
 */
public class MyFst {
	static MyFst _fst=null;
	FSTConfiguration configuration = null;
	
	public static MyFst getInstance() {
		if (_fst == null) {
			_fst=new  MyFst();
			_fst.configuration = FSTConfiguration
				    // .createDefaultConfiguration();
				            .createStructConfiguration();		
			}
		return _fst;
	}
	public  byte[] serialize(Object obj) {
		return configuration.asByteArray(obj);
	}
	public  byte[] serialize(Object obj, File file) {
		byte[] bytes = serialize(obj);
		MyFile.writeBytes(file, bytes);
		return bytes;
	}
	public  byte[] serialize(Object obj, OutputStream file) {
		byte[] bytes = serialize(obj);
		MyFile.addBytes(file, bytes);
		try {
			file.close();
			return bytes;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public  <T> T deserialize(byte[] bytes) {
		try {
			return (T) configuration. asObject(bytes);
		}catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public  <T> T deserialize(File file) {
		byte[] bytes;
		try {
			bytes = MyFile.readAllBytes(file);
		} catch (Exception e) {
			return null;
		}
		if (bytes == null)
			return null;
		return deserialize(bytes);
		
	}
	public  <T> T deserialize(InputStream file) {
		byte[] bytes;
		try {
			bytes = MyFile.readAllBytes(file);
		} catch (Exception e) {
			return null;
		}
		if (bytes == null)
			return null;
		return deserialize(bytes);
	}

}
